package com.aerozhonghuan.logisticsmall.modules.order.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aerozhonghuan.logisticsmall.R;
import com.aerozhonghuan.logisticsmall.framework.base.Constants;
import com.aerozhonghuan.logisticsmall.modules.order.beans.OrderSettlementListBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 描述 我的订单Adapter
 * @author zhangyonghui
 * 创建日期 2019-05-23
 */
public class SettlementListAdapter extends RecyclerView.Adapter<SettlementListAdapter.MyViewHolder> {

    private Context context;
    private List<OrderSettlementListBean.OrderSettlementBean> orderList;
    private OnItemClickListener onItemClickListener;

    public SettlementListAdapter(Context context) {
        this.context = context;
        orderList = new ArrayList<>();
    }

    public void addList(List<OrderSettlementListBean.OrderSettlementBean> orderList) {
        this.orderList.addAll(orderList);
        notifyDataSetChanged();
    }

    public void refreshList(List<OrderSettlementListBean.OrderSettlementBean> orderList) {
        this.orderList.clear();
        this.orderList.addAll(orderList);
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_settlement_list, null);
        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {
        final OrderSettlementListBean.OrderSettlementBean orderSettlementBean = orderList.get(position);
        if (orderSettlementBean == null || orderSettlementBean.getBill() == null || orderSettlementBean.getBill().getPlist() == null || orderSettlementBean.getBill().getPlist().size() == 0) {
            return;
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(view, orderSettlementBean);
                }
            }
        });
        OrderSettlementListBean.CommodityBean commodityListBean = orderSettlementBean.getBill().getPlist().get(0);
        holder.tvStationname.setText(TextUtils.isEmpty(orderSettlementBean.getStationName()) ? "未知" : orderSettlementBean.getStationName());
        holder.tvBaseName.setText(commodityListBean.getBaseName());
        holder.tvPrice.setText(String.format("¥ %s", (float) commodityListBean.getPrice() / 100));
        holder.tvNumber.setText(String.format("x%s", TextUtils.isEmpty(commodityListBean.getNumber()) ? "--" : commodityListBean.getNumber()));
        holder.tvTotalNumber.setText(String.format("共%s件商品", orderSettlementBean.getBill().getTotalNumber()));
        holder.tvTotalPrice.setText(String.format("¥ %s", (float) orderSettlementBean.getBill().getTotalPrice() / 100));
        holder.tvActualprice.setText(String.format("¥ %s", (float) orderSettlementBean.getBill().getActualPrice() / 100));
        if ((orderSettlementBean.getStatus() & Constants.TYPE_SETTLEMENTED) == Constants.TYPE_SETTLEMENTED) {
            holder.llCustomer.setVisibility(View.VISIBLE);
            holder.tvSettlementprice.setText(String.format("¥ %s", (float) orderSettlementBean.getCustomer().getMoney() / 100));
            holder.tvSettlementtime.setText(orderSettlementBean.getCustomer().getCreatetime());
            holder.tvStatus.setText("已结算");
        } else {
            holder.llCustomer.setVisibility(View.GONE);
            holder.tvStatus.setText("未结算");
        }
    }

    @Override
    public int getItemCount() {
        return orderList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView tvStationname, tvStatus, tvBaseName, tvPrice, tvNumber, tvTotalPrice, tvTotalNumber, tvActualprice, tvSettlementprice, tvSettlementtime;
        public ImageView imgCommodity;
        public LinearLayout llCustomer;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvStationname = (TextView) itemView.findViewById(R.id.tv_stationname);
            tvStatus = (TextView) itemView.findViewById(R.id.tv_status);
            tvBaseName = (TextView) itemView.findViewById(R.id.tv_basename);
            tvPrice = (TextView) itemView.findViewById(R.id.tv_price);
            tvNumber = (TextView) itemView.findViewById(R.id.tv_number);
            tvTotalPrice = (TextView) itemView.findViewById(R.id.tv_totalprice);
            tvTotalNumber = (TextView) itemView.findViewById(R.id.tv_totalnumber);
            tvActualprice = (TextView) itemView.findViewById(R.id.tv_actualprice);
            tvSettlementprice = (TextView) itemView.findViewById(R.id.tv_settlementprice);
            tvSettlementtime = (TextView) itemView.findViewById(R.id.tv_settlementtime);
            imgCommodity = (ImageView) itemView.findViewById(R.id.img_commodity);
            llCustomer = (LinearLayout) itemView.findViewById(R.id.ll_customer);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(View view, OrderSettlementListBean.OrderSettlementBean orderListBean);
    }

}
